﻿/**
* @private
* @license	GNU GPLv2
*
* Copyright (C) 2007  Polly Jex.
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
* MA  02110-1301, USA.
*
* @see	http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/

package com.kirika.utils {

	/**
	 * @import
	 */
	
	import com.kirika.core.CoreStatic;
	import com.kirika.utils.ArgumentsUtil;
	import com.kirika.utils.NumberUtil;
	import com.kirika.utils.TypeUtil;
	
	/**
	 * Utilidades para la manipulacion de matrices.
	 * @author	Polly Jex (pollyjex@emc2zen.com.ar)
	 */
	public class ArrayUtil extends CoreStatic {
		
		/**
		 * Retorna un nuevo Array discriminando del Array original los valor que no concuerden con el tipo.
		 * @param	value	Array original.
		 * @param	type	Valor a discriminar.
		 * @return	Array
		 */
		public static function discriminateByType( value:Array, type:* = -1 ):Array {
			
			if( type != -1 ){
				
				var arrReturn:Array = new Array();
				var arrSource:Array = ArgumentsUtil.verify( value );
				
				while( arrSource.length > 0 ){			
					if( TypeUtil.thisIs( arrSource[ 0 ], type ) ){
						arrReturn.push( arrSource.shift() );		
					}
				}
				
				if( ! TypeUtil.isEmpty( arrReturn ) ){
					return arrReturn;
				}
				
			}
			
			return null;
			
		}
		
		/**
		 * Retorna un unico valor o una lista con los valores del array pero desordenados.
		 * @param	value 	Array original.
		 * @param	noList	Define el modo de retorno. LIST:TRUE | UNIQUE:FALSE.
		 * @return	Array
		 */
		public static function random( value:Array, noList:Boolean = false ):Array {
			
			var arrReturn:Array = new Array();
			var arrSource:Array = NumberUtil.uniqueRandom( 0, value.length - 1 );
			
			while( arrSource.length > 0 ){			
				
				arrReturn.push( value[ arrSource.shift() ] );		
				
				if( noList ){
					break;
				}
				
			}		
			
			return arrReturn;
			
		}
		
	}
	
}
